﻿Public Class ucPeriodClassExamDetails
    Inherits UcBaseEditControl
    Implements IUcEditControl


#Region "Methods"

    ''' <summary>
    ''' Fills the controls on the screen from data in the object
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub _loadData() Handles Me.evLoadObjectData


        Dim mo As PeriodClass = DirectCast(Me.ModelObject(), PeriodClass)
        Me.ExamCenterId.Value = mo.ExamCenterID
        Me.ExamDate.DateValue = mo.ExamDate
        Me.ExamTimeFrom.DateValue = mo.ExamTimeFrom
        Me.ExamTimeTo.DateValue = mo.ExamTimeTo


    End Sub


    ''' <summary>
    ''' Loads the object from the database and then sets the proeperties 
    ''' of the object from values on the controls
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub _loadToObject() Handles Me.evLoadToObject

        Dim mo As PeriodClass = DirectCast(Me.ModelObject, PeriodClass)
        mo.ExamCenterID = Me.ExamCenterId.intValue
        mo.ExamDate = Me.ExamDate.DateValue
        mo.ExamTimeFrom = Me.ExamTimeFrom.DateValue
        mo.ExamTimeTo = Me.ExamTimeTo.DateValue
        
    End Sub

    Public Sub _InitializeControl() Handles Me.InitializeControl

        If Me.isInitialized = False Then
            'setup datasources here
            Dim centersDs As List(Of ExamCenter) = New BusObjects.Mappers.ExamCenterDBMapper().findAll()

            Me.ExamCenterId.DataSource = centersDs
            Me.ExamCenterId.DisplayMember = "CenterName"
            Me.ExamCenterId.ValueMember = "ExamCenterId"

            Me.isInitialized = True

        End If

    End Sub

#End Region

    Private Sub ucPeriodClassExamDetails_Validating(ByVal sender As Object, _
                            ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating

        If IsDate(Me.ExamTimeFrom) AndAlso IsDate(Me.ExamTimeTo) Then
            If CDate(Me.ExamTimeFrom.DateValue) > CDate(Me.ExamTimeTo.DateValue) Then
                Me.ErrProvider.SetError(Me, "Exam Time From must be before Exam Time To")
            End If
        End If

    End Sub
End Class
